<template>
  <div>
    <el-dialog
      :title="status.isAdd ? '分类添加' : '分类编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <!-- 绑定数据到表单 -->
      <el-form :model="form">
        {{ form }}
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="form.pid" placeholder="请选择上级分类" >
            <el-option label="顶级分类" :value="0"></el-option>
            <!-- 循环 -->
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <div class="my-upload">
            <h3 class="my-upload-plus">+</h3>
            <img v-if="imgURL" class="my-upload-img" :src="imgURL" alt="" />
            <!-- 清空数据时，form可以清空，图片地址可以清空，但是input上的文件清除不了，
            所以将input条件渲染，弹框出现，新建一个input，这样之前的input文件就可以清除不影响下一次 -->
            <input
              v-if="status.isshow"
              type="file"
              class="my-upload-input"
              @change="changeFile"
            />
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqcateadd, reqcateinfo, reqcateedit } from "../../../http/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
import path from "path";
export default {
  components: {},
  props: ["status"],
  data() {
    return {
      //1.1初始化表单数据,绑定到页面
      form: {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      // 图片地址
      imgURL: "",
    };
  },
  computed: {
    ...mapGetters({
      list: "cate/list",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "cate/reqList",
    }),

    //获取文件
    changeFile(e) {
      //得到文件
      let file = e.target.files[0];
      //文件大小类型判断
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件大小最大2M");
        return;
      }
      let imgArr = [".jpg", ".jpeg", ".png", ".gif"];
      // let houzhui=file.name.slice(file.name.lastIndexOf("."))
      // console.log(houzhui);
      let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      //根据文件生成一个文件地址并赋值给URL
      this.imgURL = URL.createObjectURL(file);

      //将文件存到form中的img中
      this.form.img = file;
    },
    //点击了取消
    cancel() {
      this.status.isshow = false;
    },
    //清空数据
    empty() {
      this.form = {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      };
      //图片单独处理
      this.imgURL = "";
    },
    //添加逻辑
    add() {
      reqcateadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //数据清空
          this.empty();
          //弹框消失
          this.status.isshow = false;
          //列表刷新...........................
          this.reqList();
        }
      });
    },
    
    //点击了编辑按钮，获取一条数据
    getOne(id) {
      reqcateinfo({
        id: id,
      }).then((res) => {
        if (res.data.code === 200) {
          this.form = res.data.list;
          //补id
          this.form.id = id;
          //给图片赋值
          this.imgURL = this.$pre + this.form.img;
        }
      });
    },
    //修改逻辑
    update() {
      reqcateedit(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          this.empty();
          this.status.isshow = false;
          this.reqList();
        }
      });
    },
    //弹框关闭
    closed() {
      //如果是添加的取消，什么都不做；如果是编辑的取消，form清空
      if (!this.status.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {},
  watch: {},
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";

.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary;
  position: relative;
}
.my-upload-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
}
.my-upload-input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
  opacity: 0;
}
.my-upload-img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
</style>